<?php
/**
 * Created by PhpStorm.
 * User: xuechunxuan
 * Date: 2018/10/17
 * Time: 11:45
 */

namespace app\index\controller;

use app\index\model\Sharehistory;
use app\index\model\User;
use think\Loader;
use think\Request;

class Session extends Base
{
    public $userInfo = "";

    //验证用户是否登陆
    public function _initialize(Request $request = null)
    {
        if (!\think\Session::has("uid")) {
            return $this->redirect("index/index");
        }
        $userInfo = $this->getUser()->findById(\think\Session::get("uid"));
        $this->userInfo = $userInfo;
       if($this->userInfo['is_lock']==1)die();//冻结用户终止进程
        $this->assign("userInfo", $userInfo);
    }


    public function getUser()
    {
        return new User();
    }

    public function getShareHistory(){
        return new Sharehistory();
    }

    /*
     * 生成二维码图片
     * @param $url string |生成对应图片的路径
     * @param $path string |对应保存的路径
     * @param $fileName string | 图片名称
     * @param $level int  |对应图片的容错等级
     * @param $size int  | 图片对应的大小
     * */
    public function createQRCode($url, $path = "", $fileName = "", $level = 3, $size = 5)
    {
        Vendor("phpqrcode.phpqrcode");
        $errorCorrectionLevel = intval($level);//容错级别
        $matrixPointSize = intval($size);//生成图片大小
        //生成二维码图片
        $object = new \QRcode();
        //不存在路径则创建
        if (!file_exists($path)) {
            mkdir($path,0777,true);
        }
        $object->png($url, $fileName, $errorCorrectionLevel, $matrixPointSize, 3);
    }

    /*
     * 合并两张图片
     * @param $img1 string | 主图片
     * @param $img2 string | 被合并图片
     * @param $path string | 保存路径
     * @param $fileName string | 保存名称
     * @param $align string |位置 T,R,L,B,C  ↑→←↓中
     * //常量，标识左上角水印
const WATER_NORTHWEST = 1;
//常量，标识上居中水印
const WATER_NORTH     = 2;
//常量，标识右上角水印
const WATER_NORTHEAST = 3;
//常量，标识左居中水印
const WATER_WEST      = 4;
//常量，标识居中水印
const WATER_CENTER    = 5;
//常量，标识右居中水印
const WATER_EAST      = 6;
//常量，标识左下角水印
const WATER_SOUTHWEST = 7;
//常量，标识下居中水印
const WATER_SOUTH     = 8;
//常量，标识右下角水印
const WATER_SOUTHEAST = 9;
     * */
    public function mergeImg($img1, $img2, $path, $fileName, $align = "WATER_CENTER")
    {
        $img = \think\Image::open($img1);

        $img->water($img2, \think\Image::WATER_CENTER,100)->save($img1);
    }

}